package com.nageoffer.shortlink.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nageoffer.shortlink.project.common.convention.result.Result;
import com.nageoffer.shortlink.project.common.convention.result.Results;
import com.nageoffer.shortlink.project.pojo.dto.Req.ShortLinkCreateReqDTO;
import com.nageoffer.shortlink.project.pojo.dto.Req.ShortLinkPageReqDTO;
import com.nageoffer.shortlink.project.pojo.dto.Req.ShortLinkUpdateReqDTO;
import com.nageoffer.shortlink.project.pojo.dto.Resp.ShortLinkCreateRespDTO;
import com.nageoffer.shortlink.project.pojo.dto.Resp.ShortLinkGroupCountRespDTO;
import com.nageoffer.shortlink.project.pojo.dto.Resp.ShortLinkPageRespDTO;
import com.nageoffer.shortlink.project.service.ShortLinkService;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
//@RequestMapping("/api/short-link/project/v1/shortlink")
@RequiredArgsConstructor
public class ShortLinkController {

    private final ShortLinkService shortLinkService;

    /**
     * 新增短链接 TODO 有个bug在创建type为1的时候，传日期传不上去
     * @param requestParam
     * @return
     */
    @PostMapping("/api/short-link/project/v1/shortlink/create")
    public Result<ShortLinkCreateRespDTO> createShortLink(@RequestBody ShortLinkCreateReqDTO requestParam){
        return Results.success(shortLinkService.createShortLink(requestParam));
    }

    /**
     * 分页查找短链接
     */
    @PostMapping("/api/short-link/project/v1/shortlink/page")
    public Result<IPage<ShortLinkPageRespDTO>> pageShortLink(@RequestBody ShortLinkPageReqDTO requestParam){

        return Results.success(shortLinkService.pageShortLink(requestParam));
    }

    /**
     * 分组查询短链接数目
     * @param gidList
     * @return
     */
    @GetMapping("/api/short-link/project/v1/shortlink/count")
    public Result<List<ShortLinkGroupCountRespDTO>> listGroupShortLinkCount(@RequestParam("gidList") List<String> gidList){
        return Results.success(shortLinkService.listGroupShortLinkCount(gidList));
    }

    /**
     * 修改短链接信息
     * @param requestParam
     * @return
     */
    @PutMapping("/api/short-link/project/v1/shortlink/update")
    public Result<Void> updateShortLink(@RequestBody ShortLinkUpdateReqDTO requestParam){
        shortLinkService.updateShortLink(requestParam);
        return Results.success();
    }

    /**
     * 短链接核心功能，从短链接跳转原始链接
     * @param shortUri
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/{shortUri}")
    public Result<Void> restoreUrl(@PathVariable String shortUri, ServletRequest request, ServletResponse response) throws IOException {
        shortLinkService.restoreUrl(shortUri,request,response);
        return Results.success();
    }


}
